import { createUniFetch } from 'uni-app-fetch'
import { useMyStore } from '../stores'
import { showFailToast } from '../utils'

export const uniFetch = createUniFetch({
	loading: { title: '正在加载当中……' },
	baseURL: 'https://slwl-api.itheima.net',

	intercept: {
		request(options) {
      const store = useMyStore()
      const defaultHeaders = {
        Authorization:store.token,
      }
      options.header = Object.assign({},defaultHeaders,options.header)
			return options
		},
		response(result) {
      if(result.statusCode === 401){
        showFailToast('请先登录')
        uni.navigateTo({
          url:'/pages/login/index'
        })
      }
			return result
		},
	},
})


